package com.Bigbluebook.ClickEvent;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.Bigbluebook.Activity.PostDetailsActivity;
import com.Bigbluebook.R;
import com.Bigbluebook.Service.FollowApiService;
import com.Bigbluebook.Service.PostApiService;
import com.Bigbluebook.entity.Follow;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ButtonClickListener implements View.OnClickListener {
    private Context context;
    private PostApiService postApiService;
    private EditText titleView;
    private EditText contentView;
    private Spinner categorySpinner;
    private Intent intent;

    // 构造函数
    public ButtonClickListener(Context context,Intent intent) {
        this.context = context;
        this.intent = intent;
        if (intent != null) {
            postApiService = new PostApiService();
        } else {
            Log.e("ButtonClickListener", "Intent is null in constructor");
        }
    }

    // 初始化控件
    public void initializeUI(PostDetailsActivity activity) {
        // 从名为PostDetailsActivity的Activity类中的实例context中
        // 寻找相应的控件，并赋值给成员变量

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnFollow) {
            handleFollowClick();
        } else if (v.getId() == R.id.btnSendComment) {
            handleSendCommentClick();
        } else if (v.getId() == R.id.btnLike) {
            handleLikeClick();
        } else if (v.getId() == R.id.btnStar) {
            handleStarClick();
        } else if (v.getId() == R.id.btnViewComments) {
            handleViewCommentsClick();
        }
        // 如果没有匹配到任何ID，则不需要做任何处理，因为这里没有else块
    }

    private void handleFollowClick() {
        // 处理关注按钮点击事件
        FollowApiService followApiService = new FollowApiService();
        SharedPreferences sharedPreferences = context.getSharedPreferences("user_info", Context.MODE_PRIVATE);
        int userId = sharedPreferences.getInt("userid", -1);
        int follower_id = userId;
        int followee_id = intent.getIntExtra("userid",-1);
        Log.e("检测参数", String.valueOf(follower_id + followee_id));
        followApiService.followUser(follower_id, followee_id, new Callback<Follow>() {
                    @Override
                    public void onResponse(Call<Follow> call, Response<Follow> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            Log.i("关注",response.toString());
                            Toast.makeText(context, "关注成功", Toast.LENGTH_SHORT).show();
                            Log.e("PostDetailsActivity", "关注成功");
                        } else {
                            Log.e("PostDetailsActivity", "关注失败");
                            Log.e("关注接口",response.toString());
                            Toast.makeText(context, "关注失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Follow> call, Throwable t) {
                        Log.e("PostDetailsActivity", "网络请求失败: " + t.getMessage());
                        Toast.makeText(context, "网络请求失败", Toast.LENGTH_SHORT).show();
                    }
        });

    }
//发送评论
    private void handleSendCommentClick() {
        // 获取标题、内容和分类

    }

    private void handleLikeClick() {
        // 处理点赞按钮点击事件

        Toast.makeText(context, "点赞", Toast.LENGTH_SHORT).show();
    }

    private void handleStarClick() {
        // 处理收藏按钮点击事件
        Toast.makeText(context, "收藏", Toast.LENGTH_SHORT).show();
    }

    private void handleViewCommentsClick() {
        // 处理查看评论按钮点击事件
        Toast.makeText(context, "查看评论", Toast.LENGTH_SHORT).show();
    }

    public boolean check(String title, String content, String category) {
        // 检查标题、内容和分类是否有效
        return !(title.isEmpty() || content.isEmpty() || category.isEmpty());
    }
}